package com.lifp.thread.lock.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description 读写锁的降级
 * @Author lfp
 * @Date 2022/8/5
 * @Version 1.0
 */
public class ReadWriteLockDegradation {

    public static void main(String[] args) {

        //可重入读写锁对象
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();//读锁
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();//写锁

        //锁降级
        //1、写锁加锁
        writeLock.lock();
        System.out.println("hello world");

        //2、读锁加锁
        readLock.lock();
        System.out.println("---read");

        //3、释放写锁
        writeLock.unlock();
        //4、释放读锁
        readLock.unlock();
    }
}
